﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace 前后都有数字的内容提取
{
    class Program
    {
        static void Main(string[] args)
        {

            /*
             input: B,EFD
            \LT KIL
             
            // */
            //Console.WriteLine("Hello World!");
            //TestRegex testRegex = new TestRegex();

            //var str = " 一 二 三 四      五  六  ".Trim();//自己trim

            //int count = new Regex(@"\s+", RegexOptions.Compiled).Matches(str).Count;
            //var arrayList = Regex.Split(str, @"\s+");
            //var dic = Enumerable.Range(0, 5).ToDictionary(x => string.Join(' ', arrayList.Skip(0).Take(x + 1).ToList()), x => string.Join(' ', arrayList.Skip(x + 1).ToList()));

            //dic.Keys.ToList().ForEach(key => { Console.WriteLine(key + "--------" + dic[key]); });
            //str.LastIndexOf
            //Enumerable.Range(0, 5).ToDictionary(x =>
            //{ 

            //}, x => { return ""; }
            //);


            //for (var i = 0; i < len; i++)
            //{

            //}


            //var a = new LotNumberSearchText(@"KIL1432A 1432 A&B B,4,A,MTC");
            //Console.WriteLine(a);
            //var b = new LotNumberSearchText(@"KIL1432A 1432 ");
            //Console.WriteLine(b);
            //var c = new LotNumberSearchText(@"KIL1432A 1432 A&B ");
            //Console.WriteLine(c);


            ////var b = new RoadJuctionText(@"SS");
            ////Console.WriteLine(b);
            ////var b = new LotNumberSearchTextVersion2("KIL 1432 A");
            ////Console.WriteLine(b);
            ////var c = new LotNumberSearchTextVersion2("KIL 1432 ");
            ////Console.WriteLine(c);
            ///---------------------------------------
            ///

            Console.WriteLine(new RouteAndExitSearchText("1,aa"));
            Console.WriteLine(new RouteAndExitSearchText("1 ,aa"));
            Console.WriteLine(new RouteAndExitSearchText("1, aa"));
            Console.WriteLine(new RouteAndExitSearchText("1 , aa"));
            Console.WriteLine(new RouteAndExitSearchText("1 , aa "));
            Console.WriteLine(new RouteAndExitSearchText(" 1 , aa "));
            Console.WriteLine(new RouteAndExitSearchText(" 1 , aa "));
            Console.WriteLine(new RouteAndExitSearchText(" 1 , aa bb  "));
            Console.WriteLine(new RouteAndExitSearchText(" 1 , aa bb cc "));



            ////var list = new ForDuck().ProcessDuckData();
            Console.Read();
        }
    }

    public class TestRegex
    {
        public TestRegex()
        {
            var str = "https://www.runoob.com/try/try.php?filename=try,jsref_regexp4#";
            Console.WriteLine(str);
            var regexText = @"(\w+):\/\/([^/:]+)(:\d*)?([^#]*)";
            var matchResult = Regex.Match(str, regexText);

            var str1 = "KIL 1432 1432 B,4,A,MTC";
            var regexText1 = @"(\w+)  ([^,]+)?([^ ]*)";
            var matchResult1 = Regex.Match(str1, regexText1);

            for (var i = 0; i < matchResult.Groups.Count; i++)
            {
                Console.WriteLine(matchResult.Groups[i].Value);
            }

            Console.WriteLine("|||||||||||||||||||||||||||||||||||||||||");
            for (var i = 0; i < matchResult1.Groups.Count; i++)
            {
                Console.WriteLine(matchResult1.Groups[i].Value);
            }
        }
    }

    public class RoadJuctionText
    {
        //private static readonly string FullSearchTextRegex = @"((?<" + nameof(Abbreviation) + @">\w+)" +
        //    //@"\s(?<" + nameof(SectionCode) + @">(\w+)\,)" +
        //    ")";

        //private static readonly string FullSearchTextRegex = @"((?<Abbreviation>\w+)(?<LotPart>[\s\S]+)(?<SectionCode>))";
        private static readonly string FullSearchTextRegex = @$"((?<{nameof(StreetName1)}>\w+)+/*(?<{nameof(StreetName2)}>\w+)*)";
        private string searchText;
        public RoadJuctionText(string searchText)
        {

            if (Regex.IsMatch(searchText, FullSearchTextRegex))
            {
                this.searchText = searchText;
                var matchResult = Regex.Match(searchText, FullSearchTextRegex);

                StreetName1 = matchResult.Groups.ContainsKey(nameof(StreetName1)) ? matchResult.Groups[nameof(StreetName1)].Value.Trim() : null;
                StreetName2 = matchResult.Groups.ContainsKey(nameof(StreetName2)) ? matchResult.Groups[nameof(StreetName2)].Value.Trim() : null;

            }

            //if (!string.IsNullOrWhiteSpace(LotPart))
            //{


            //  LotNumber = LotPart;

            //}

        }
        /// <summary>
        /// 对应lot_abbreviation
        /// </summary>
        public string StreetName1 { get; set; }

        public string StreetName2 { get; set; }
        /// <summary>
        /// 对应lot_number
        /// </summary>


        public override string ToString() => $"searchText {searchText}解析为"
            + $"{nameof(this.StreetName1)}={this.StreetName1}  " +
            $"{nameof(this.StreetName2)}={this.StreetName2} ";
    }
    public class LotNumberSearchTextVersion2
    {
        //private static readonly string FullSearchTextRegex = @"((?<" + nameof(Abbreviation) + @">\w+)" +
        //    //@"\s(?<" + nameof(SectionCode) + @">(\w+)\,)" +
        //    ")";

        //private static readonly string FullSearchTextRegex = @"((?<Abbreviation>\w+)(?<LotPart>[\s\S]+)(?<SectionCode>))";
        private static readonly string FullSearchTextRegex = @"((?<Abbreviation>\w+))";
        private string searchText;
        public LotNumberSearchTextVersion2(string searchText)
        {

            if (Regex.IsMatch(searchText, FullSearchTextRegex))
            {
                this.searchText = searchText;
                var matchResult = Regex.Match(searchText, FullSearchTextRegex);

                Abbreviation = matchResult.Groups.ContainsKey(nameof(Abbreviation)) ? matchResult.Groups[nameof(Abbreviation)].Value.Trim() : null;
                LotPart = matchResult.Groups.ContainsKey(nameof(LotPart)) ? matchResult.Groups[nameof(LotPart)].Value.Trim() : null;
                SectionCode = matchResult.Groups.ContainsKey(nameof(SectionCode)) ? matchResult.Groups[nameof(SectionCode)].Value.Trim() : null;

            }

            //if (!string.IsNullOrWhiteSpace(LotPart))
            //{


            //  LotNumber = LotPart;

            //}

        }
        /// <summary>
        /// 对应lot_abbreviation
        /// </summary>
        public string Abbreviation { get; set; }

        private string LotPart { get; set; }
        /// <summary>
        /// 对应lot_number
        /// </summary>
        public string LotNumber { get; set; }
        /// <summary>
        /// 对应lot_number
        /// </summary>
        public string LotAlpha { get; set; }
        /// <summary>
        /// 对应lot_number
        /// </summary>
        public string SectionCode { get; set; }

        public override string ToString() => $"searchText {searchText}解析为"
            + $"{nameof(this.Abbreviation)}={this.Abbreviation}  " +
            $"{nameof(this.LotNumber)}={this.LotNumber} " +
            $"{nameof(this.LotAlpha)}={this.LotAlpha} " +
            $"{nameof(this.SectionCode)}={this.SectionCode} ";
    }



    public class RouteAndExitSearchText
    {
    //    private static readonly string RouteAndExitTextFullRegex =
    //        @"((?<" + nameof(RouteNumber) + @">\d+)(?<" + nameof(ExitInfoOrStreetName) + @">\w+))";//后面有数字的
        //private static readonly string RouteAndExitTextFullRegex = @"((?<RouteNumber>\d+)\,(?<ExitInfoOrStreetName>[a-z]+))";//后面有数字的

        private static readonly string RouteAndExitTextFullRegex = @"(?<" + nameof(RouteNumber) + @">^[\d\s]+)(\,(?<" + nameof(ExitInfoOrStreetName) + @">([\u4e00-\u9fa5_a-zA-Z0-9\s\.\~\/\-]+)))";//后面有数字的

        private RouteAndExitSearchText() { }
        public RouteAndExitSearchText(string searchText)
        {
            if (Regex.IsMatch(searchText, RouteAndExitTextFullRegex))
            {
                var matchResult = Regex.Match(searchText, RouteAndExitTextFullRegex);
                this.RouteNumber = matchResult.Groups.ContainsKey(nameof(this.RouteNumber))
                ? matchResult.Groups[nameof(this.RouteNumber)].Value.Trim() : null;
                this.ExitInfoOrStreetName = matchResult.Groups.ContainsKey(nameof(this.ExitInfoOrStreetName))
                ? matchResult.Groups[nameof(this.ExitInfoOrStreetName)].Value.Trim() : null;
            }
            else
            {
                this.Text = searchText.Trim();
            }
        }


        public string RouteNumber { get; set; }
        public string ExitInfoOrStreetName { get; set; }


        /// <summary>
        /// 对应Text
        /// </summary>
        public string Text { get; set; }

        public override string ToString() => $"{nameof(RouteNumber)}={this.RouteNumber} {nameof(ExitInfoOrStreetName)}={this.ExitInfoOrStreetName}";
    }
    public class LotNumberSearchText
    {
        private static readonly string FullSearchTextRegex = @"((?<Abbreviation>[a-zA-Z0-9]+)\s(?<LotNumber>[a-zA-Z0-9]+)\s(?<LotAlpha>[a-zA-Z&0-9]+)\s(?<SectionCode>[a-zA-Z0-9\,]*))";
        private static readonly string ShortSearchTextRegex = @"((?<Abbreviation>[a-zA-Z0-9]+)\s(?<LotNumber>[a-zA-Z0-9]+))";
        private static readonly string SearchTextRegex = @"((?<Abbreviation>[a-zA-Z0-9]+)\s(?<LotNumber>[a-zA-Z0-9]+)\s(?<LotAlpha>[a-zA-Z&0-9]+))";



        private string searchText;
        public LotNumberSearchText(string searchText)
        {

            if (Regex.IsMatch(searchText, FullSearchTextRegex))
            {
                this.searchText = searchText;
                var matchResult = Regex.Match(searchText, FullSearchTextRegex);

                Abbreviation = matchResult.Groups.ContainsKey(nameof(Abbreviation)) ? matchResult.Groups[nameof(Abbreviation)].Value.Trim() : null;
                LotNumber = matchResult.Groups.ContainsKey(nameof(LotNumber)) ? matchResult.Groups[nameof(LotNumber)].Value.Trim() : null;
                LotAlpha = matchResult.Groups.ContainsKey(nameof(LotAlpha)) ? matchResult.Groups[nameof(LotAlpha)].Value.Trim() : null;
                SectionCode = matchResult.Groups.ContainsKey(nameof(SectionCode)) ? matchResult.Groups[nameof(SectionCode)].Value.Trim() : null;

            }
            else if (Regex.IsMatch(searchText, SearchTextRegex))
            {
                this.searchText = searchText;
                var matchResult = Regex.Match(searchText, ShortSearchTextRegex);
                Abbreviation = matchResult.Groups.ContainsKey(nameof(Abbreviation)) ? matchResult.Groups[nameof(Abbreviation)].Value.Trim() : null;
                LotNumber = matchResult.Groups.ContainsKey(nameof(LotNumber)) ? matchResult.Groups[nameof(LotNumber)].Value.Trim() : null;
                LotAlpha = matchResult.Groups.ContainsKey(nameof(LotAlpha)) ? matchResult.Groups[nameof(LotAlpha)].Value.Trim() : null;
            }
            else if (Regex.IsMatch(searchText, ShortSearchTextRegex))
            {
                this.searchText = searchText;
                var matchResult = Regex.Match(searchText, ShortSearchTextRegex);
                Abbreviation = matchResult.Groups.ContainsKey(nameof(Abbreviation)) ? matchResult.Groups[nameof(Abbreviation)].Value.Trim() : null;
                LotNumber = matchResult.Groups.ContainsKey(nameof(LotNumber)) ? matchResult.Groups[nameof(LotNumber)].Value.Trim() : null;
            }


        }
        /// <summary>
        /// 对应lot_abbreviation
        /// </summary>
        public string Abbreviation { get; set; }

        /// <summary>
        /// 对应lot_number
        /// </summary>
        public string LotNumber { get; set; }
        /// <summary>
        /// 对应lot_number
        /// </summary>
        public string LotAlpha { get; set; }
        /// <summary>
        /// 对应lot_number
        /// </summary>
        public string SectionCode { get; set; }

        public override string ToString() => $"searchText {searchText}解析为"
            + $"{nameof(this.Abbreviation)}={this.Abbreviation}  " +
            $"{nameof(this.LotNumber)}={this.LotNumber} " +
            $"{nameof(this.LotAlpha)}={this.LotAlpha} " +
            $"{nameof(this.SectionCode)}={this.SectionCode} ";
    }



    public class AddressSearchText
    {
        private static readonly string SearchTextRegexStuffix = @"(?<Text>([\u4e00-\u9fa5_a-zA-Z0-9\s\,\.\~\/\-]+))(?<" + nameof(EndHouseNo) + @">\s([0-9])*$)";//后面有数字的

        private static readonly string SearchTextRegexPrefix = @"(?<" + nameof(StartHouseNo) + @">([0-9]{1,})\s)(?<Text>([\u4e00-\u9fa5_a-zA-Z0-9\s\,\.\~\/\-]+)$)";

        //private static readonly string SearchTextRegex = @"(?<" + nameof(StartHouseNo) + @">([0-9])*)(?<Text>([\u4e00-\u9fa5_a-zA-Z0-9\s\,\.\~\/\-]+))(?<" + nameof(EndHouseNo) + @">\s*([0-9])*$)";

        private AddressSearchText() { }
        public AddressSearchText(string searchText)
        {
            if (Regex.IsMatch(searchText, SearchTextRegexPrefix))
            {
                var matchResult = Regex.Match(searchText, SearchTextRegexPrefix);

                this.StartHouseNo = matchResult.Groups.ContainsKey(nameof(this.StartHouseNo))
                ? matchResult.Groups[nameof(this.StartHouseNo)].Value.Trim() : null;
                this.Text = matchResult.Groups.ContainsKey(nameof(this.Text))
                ? matchResult.Groups[nameof(this.Text)].Value.Trim() : null;

                Console.WriteLine("有前缀HouseNumber的");

            }

            else if (Regex.IsMatch(searchText, SearchTextRegexStuffix))
            {
                var matchResult = Regex.Match(searchText, SearchTextRegexStuffix);

                this.EndHouseNo = matchResult.Groups.ContainsKey(nameof(this.EndHouseNo))
                ? matchResult.Groups[nameof(this.EndHouseNo)].Value.Trim() : null;
                this.Text = matchResult.Groups.ContainsKey(nameof(this.Text))
                ? matchResult.Groups[nameof(this.Text)].Value.Trim() : null;

                Console.WriteLine("有后缀HouseNumber的");

            }
            else
            {
                this.Text = searchText.Trim();
            }

        }
        public bool DoesContainHouseNo => !string.IsNullOrWhiteSpace(this.StartHouseNo) || !string.IsNullOrWhiteSpace(this.EndHouseNo);

        public string StartHouseNo { get; set; }
        public string EndHouseNo { get; set; }


        /// <summary>
        /// 对应Text
        /// </summary>
        public string Text { get; set; }

        public override string ToString() => $"{nameof(StartHouseNo)}={this.StartHouseNo} {nameof(EndHouseNo)}={this.EndHouseNo} {nameof(Text)}={Text}";
    }
}
